﻿using System;

namespace PortalCurriculo.MvcWebUI.Extensions
{
    public static class DateTimeExtensions
    {
        public static string ToRelativeDateString(this DateTime date)
        {
            return GetRelativeDateValue(date, DateTime.Now);
        }

        public static string ToRelativeDateStringUtc(this DateTime date)
        {
            return GetRelativeDateValue(date, DateTime.UtcNow);
        }

        private static string GetRelativeDateValue(DateTime date, DateTime comparedTo)
        {
            var diff = comparedTo.Subtract(date);

            if (diff.Days >= 7)
                return date.ToString("dd/MM/yyyy");
            if (diff.Days > 1)
                return string.Concat(diff.Days, " dias atrás");
            if (diff.Days == 1)
                return "ontem";
            if (diff.Hours >= 2)
                return string.Concat(diff.Hours, " horas atrás");
            if (diff.Minutes >= 60)
                return "mais de uma hora atrás";
            if (diff.Minutes >= 5)
                return string.Concat(diff.Minutes, " minutos atrás");

            return diff.Minutes >= 1 ? "poucos minutos atrás" : "menos de um minuto atrás";
        }
    }
}